/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.portlet.visitor.impl;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.View;

import com.genia.toolbox.basics.bean.Delimiter;
import com.genia.toolbox.basics.bean.DisplayObject;
import com.genia.toolbox.basics.exception.technical.TechnicalIOException;
import com.genia.toolbox.basics.manager.CollectionManager;
import com.genia.toolbox.web.portlet.bean.PortletView;

public class DumbView
    implements View
{

  private String prefix;

  private String suffix;

  private String separator;

  private CollectionManager collectionManager;



  public String getContentType()
  {
    return "text/html";
  }



  @SuppressWarnings("unchecked")
  public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response)
      throws Exception
  {
    final Collection<PortletView> portletViews = (Collection<PortletView>) model.get(RenderingPortletVisitor.PORTLET_VIEWS_ATTRIBUTE_KEY);

    getCollectionManager().join(response.getWriter(), portletViews, new Delimiter()
    {

      public void appendHead(final Appendable appendable)
          throws TechnicalIOException
      {
        try {
          appendable.append(getPrefix());
        }
        catch (final IOException e) {
          throw new TechnicalIOException(e);
        }
      }



      public void appendSeparator(final Appendable appendable)
          throws TechnicalIOException
      {
        try {
          appendable.append(getSeparator());
        }
        catch (final IOException e) {
          throw new TechnicalIOException(e);
        }
      }



      public void appendTail(final Appendable appendable)
          throws TechnicalIOException
      {
        try {
          appendable.append(getSuffix());
        }
        catch (final IOException e) {
          throw new TechnicalIOException(e);
        }
      }

    }, new DisplayObject<PortletView>()
    {

      public void append(final Appendable appendable, final PortletView portlet)
          throws IOException, TechnicalIOException
      {
        appendable.append(portlet.getIdentifier()).append(portlet.getText());
      }

    });
  }



  /**
   * getter for the prefix property.
   * 
   * @return the prefix
   */
  public String getPrefix()
  {
    return prefix;
  }



  /**
   * setter for the prefix property.
   * 
   * @param prefix
   *          the prefix to set
   */
  public void setPrefix(final String prefix)
  {
    this.prefix = prefix;
  }



  /**
   * getter for the separator property.
   * 
   * @return the separator
   */
  public String getSeparator()
  {
    return separator;
  }



  /**
   * setter for the separator property.
   * 
   * @param separator
   *          the separator to set
   */
  public void setSeparator(final String separator)
  {
    this.separator = separator;
  }



  /**
   * getter for the suffix property.
   * 
   * @return the suffix
   */
  public String getSuffix()
  {
    return suffix;
  }



  /**
   * setter for the suffix property.
   * 
   * @param suffix
   *          the suffix to set
   */
  public void setSuffix(final String suffix)
  {
    this.suffix = suffix;
  }



  /**
   * getter for the collectionManager property.
   * 
   * @return the collectionManager
   */
  public CollectionManager getCollectionManager()
  {
    return collectionManager;
  }



  /**
   * setter for the collectionManager property.
   * 
   * @param collectionManager
   *          the collectionManager to set
   */
  public void setCollectionManager(final CollectionManager collectionManager)
  {
    this.collectionManager = collectionManager;
  }

}
